using NUnit.Framework;
using Pyxis.FluentSelenium.Conditions;
using Pyxis.FluentSelenium.Locator;
using Selenium;

namespace Pyxis.FluentSelenium.SampleWebAppTest.Shopper
{
    [TestFixture]
    public class ShopperSearchTest
    {
        private WebApplication shopOnline;
        private User shopper;

        private readonly InputLocator searchCatalogText = InputLocator.WithId("searchText");
        private readonly ButtonLocator searchCatalogButton = ButtonLocator.WithId("searchButton");
        private readonly PageLoadWaitCondition searchResultPageLoad = PageLoadWaitCondition.For("Shopping/searchResults.aspx");
        private readonly ElementLocator searchResultsSummary = ElementLocator.WithId("searchResultsSummary");
        private readonly ListLocator searchResults = ListLocator.WithId("searchResults");
        private readonly ElementLocator matchingProductTitle = ElementLocator.WithId("title");

        [Test]
        public void TestSearchForProductsFindMatchingProducts()
        {
            shopper.Goto("Shopping/ShopOnline.aspx");
            shopper.For(searchCatalogText).Enters("mouse");
            shopper.For(searchCatalogButton).Clicks();
            shopper.WaitFor(searchResultPageLoad);

            shopper.For(searchResultsSummary).ShouldSee("1 match found");
            shopper.For(searchResults).ShouldCountItems(1);
            shopper.For(searchResults).Item(1).For(matchingProductTitle).ShouldSee("low-tech mouse");
        }

        [Test]
        public void TestSearchForProductsFindMatchingProductsWithSelenium()
        {
            var selenium = shopper.Selenium;
            selenium.Open("Shopping/ShopOnline.aspx");
            selenium.WaitForPageToLoad("1000");

            selenium.Type("//*[@id='searchText']", "mouse");

            shopper.For(searchCatalogButton).Clicks();
            selenium.Click("//*[@id='searchButton']");
            selenium.WaitForPageToLoad("1000");

            Assert.AreEqual("1 match found", selenium.GetText("//*[@id='searchResultsSummary']"));
            Assert.AreEqual(1, selenium.GetXpathCount("//*[@id='searchResults']/li"));
            Assert.AreEqual("low-tech mouse", selenium.GetText("//*[@id='searchResults']/li[1]"));
        }


        [Test]
        [Ignore]
        public void TestSearchResultsShowProductInformation()
        {
            Assert.Fail();
        }

        [Test]
        public void TestSearchFindingNoResultsAdvisesUser()
        {
            shopper.Goto("Shopping/ShopOnline.aspx");
            shopper.For(searchCatalogText).Enters("blah");
            shopper.For(searchCatalogButton).Clicks();
            shopper.WaitFor(searchResultPageLoad);

            shopper.For(searchResultsSummary).ShouldSee("0 matches found");
            shopper.For(searchResults).ShouldCountItems(0);
        }


        [SetUp]
        public void Initialise()
        {
            shopOnline = new WebApplication(3000, "Pyxis.FluentSelenium.SampleWebApp");
            shopOnline.Start();

            var selenium = new DefaultSelenium("127.0.0.1", 4445, "*iexplore", "http://localhost:3000/");
            shopper = new User(selenium);
            shopper.Start();
        }

        [TearDown]
        public void Cleanup()
        {
            shopper.Stop();
            shopOnline.Stop();
        }
    }
}